package leetcode.editor.cn;

//Java：删除链表的倒数第 N 个结点
public class SLwz0R {
    public static void main(String[] args) {
        Solution solution = new SLwz0R().new Solution();
        // TO TEST
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     * int val;
     * ListNode next;
     * ListNode() {}
     * ListNode(int val) { this.val = val; }
     * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
     * }
     */
    class Solution {
        public ListNode removeNthFromEnd(ListNode head, int n) {
            ListNode pre = new ListNode(-1, head);
            ListNode start = pre;
            ListNode end = pre;
            while (n != 0) {
                end = end.next;
                n--;
            }

            while (end.next != null) {
                start = start.next;
                end = end.next;
            }
            start.next = start.next.next;
            return pre.next;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}